package com.wifi.books.servlet;

import com.alibaba.fastjson.JSON;
import com.wifi.annotation.ShenZhouWifiMethod;
import com.wifi.base.BaseServlet;

import com.wifi.books.entity.BookEntity;
import com.wifi.books.service.BookTypeService;
import com.wifi.books.service.IBookService;
import com.wifi.books.service.impl.BookTypeServiceImpl;
import com.wifi.books.service.impl.IBookServiceImpl;
import com.wifi.unti.PageInfo;
import com.wifi.unti.StringUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
import java.util.Objects;


@WebServlet("/book/*")
public class BookListServlet extends BaseServlet {
    IBookService iBookService = new IBookServiceImpl();
    BookTypeService bookTypeService = new BookTypeServiceImpl();

    @ShenZhouWifiMethod("/booklist")
    public void hotBookList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Object> list = null;
        String pageNumStr = request.getParameter("pageNum");
        String pageSizeStr = request.getParameter("pageSize");
        Integer integer = iBookService.booksCount();
        PageInfo pageInfo = new PageInfo(pageNumStr, pageSizeStr, integer);
        System.out.println(pageInfo.getTotalPage());
        try {
            list = iBookService.queryBookList(pageInfo.getStart(), pageInfo.getPageSize());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        if (list == null) {
            System.out.println("啥也没有");
        }
        request.setAttribute("booklist", list);
        request.setAttribute("pageInfo", pageInfo);
        request.getRequestDispatcher("/WEB-INF/book/booklist.jsp").forward(request, response);
    }

    @ShenZhouWifiMethod("/queryBook")
    public void queryBookList(HttpServletRequest request, HttpServletResponse response) throws SQLException, IOException {
        List<Object> list = null;
        String pageNumStr = request.getParameter("pageNum");
        String pageSizeStr = request.getParameter("pageSize");
        Integer integer = iBookService.booksCount();
        PageInfo pageInfo = new PageInfo(pageNumStr, pageSizeStr, integer);
        list = iBookService.queryBookList(pageInfo.getStart(), pageInfo.getPageSize());
        pageInfo.setList(list);
        response.getWriter().write(JSON.toJSONString(pageInfo));
//        response.getWriter().write(JSON.toJSONString(list));
    }

    @ShenZhouWifiMethod("/type")
    public void queryBookType(HttpServletRequest request, HttpServletResponse response) throws SQLException, IOException {
        List<Object> list = null;
        list = bookTypeService.bookTypeList();
        response.getWriter().write(JSON.toJSONString(list));
    }

    @ShenZhouWifiMethod("/secondType")
    public void queryBookSecondType(HttpServletRequest request, HttpServletResponse response) throws SQLException, IOException {
        List<Object> list = null;
        String ftypeIdStr = request.getParameter("ftypeId");
        System.out.println(ftypeIdStr + "=====");
        list = bookTypeService.bookSecondTypeList(Integer.valueOf(ftypeIdStr));
        response.getWriter().write(JSON.toJSONString(list));
    }

    @ShenZhouWifiMethod("/queryBookById")
    public void queryBook(HttpServletRequest request, HttpServletResponse response) throws SQLException, IOException {
        String bookId = request.getParameter("bookId");
        BookEntity book = iBookService.queryBookByBookId(Integer.valueOf(bookId));
        response.getWriter().write(JSON.toJSONString(book));
    }

    @ShenZhouWifiMethod("/updateBook")
    public void updateBook(HttpServletRequest request, HttpServletResponse response) throws SQLException, IOException {
        String bookNameStr = request.getParameter("bookName");
        String bookAuthorStr = request.getParameter("bookAuthor");
        String wordCountStr = request.getParameter("wordCount");
        String statusStr = request.getParameter("status");
        String isVipStr = request.getParameter("isVip");
        String statusUpStr = request.getParameter("statusUp");
        String ftypeIdStr = request.getParameter("ftypeId");
        String stypeIdStr = request.getParameter("stypeId");
        String coverUrlStr = request.getParameter("cover_url");
        String descriptionStr = request.getParameter("description");
        String bookIdStr = request.getParameter("bookId");
        if (StringUtil.isEmpty(bookNameStr) || StringUtil.isEmpty(bookAuthorStr) || StringUtil.isEmpty(wordCountStr) ||
                StringUtil.isEmpty(statusStr) || StringUtil.isEmpty(isVipStr) || StringUtil.isEmpty(statusUpStr) ||
                StringUtil.isEmpty(ftypeIdStr) || StringUtil.isEmpty(stypeIdStr) || StringUtil.isEmpty(coverUrlStr) ||
                StringUtil.isEmpty(descriptionStr) || StringUtil.isEmpty(bookIdStr)) {
            response.getWriter().write(JSON.toJSONString(0));
            return;
        }
        BookEntity book = new BookEntity(bookNameStr, bookAuthorStr, coverUrlStr, descriptionStr, wordCountStr, Integer.valueOf(ftypeIdStr)
                , Integer.valueOf(stypeIdStr), Integer.valueOf(statusStr), Integer.valueOf(isVipStr), Integer.valueOf(statusUpStr));
        Integer updateBookI = iBookService.updateBook(Integer.valueOf(bookIdStr), book);
        response.getWriter().write(JSON.toJSONString(updateBookI));
    }

    @ShenZhouWifiMethod("/addBook")
    public void addBook(HttpServletRequest request, HttpServletResponse response) throws SQLException, IOException {
        String bookNameStr = request.getParameter("bookName");
        String bookAuthorStr = request.getParameter("bookAuthor");
        String wordCountStr = request.getParameter("wordCount");
        String statusStr = request.getParameter("status");
        String isVipStr = request.getParameter("isVip");
        String statusUpStr = request.getParameter("statusUp");
        String ftypeIdStr = request.getParameter("ftypeId");
        String stypeIdStr = request.getParameter("stypeId");
        String coverUrlStr = request.getParameter("cover_url");
        String descriptionStr = request.getParameter("description");
        String bookIdStr = request.getParameter("bookId");

        if (StringUtil.isEmpty(bookNameStr) || StringUtil.isEmpty(bookAuthorStr) || StringUtil.isEmpty(wordCountStr) ||
                StringUtil.isEmpty(statusStr) || StringUtil.isEmpty(isVipStr) || StringUtil.isEmpty(statusUpStr) ||
                StringUtil.isEmpty(ftypeIdStr) || StringUtil.isEmpty(stypeIdStr) || StringUtil.isEmpty(coverUrlStr) ||
                StringUtil.isEmpty(descriptionStr) || StringUtil.isEmpty(bookIdStr)) {
            response.getWriter().write(JSON.toJSONString(0));
            return;
        }
        BookEntity book = new BookEntity(Integer.valueOf(bookIdStr), bookNameStr, bookAuthorStr, coverUrlStr, descriptionStr, wordCountStr, Integer.valueOf(ftypeIdStr)
                , Integer.valueOf(stypeIdStr), Integer.valueOf(statusStr), Integer.valueOf(isVipStr), Integer.valueOf(statusUpStr));
        Integer updateBookI = iBookService.addBook(book);
        response.getWriter().write(JSON.toJSONString(updateBookI));
    }

//    @ShenZhouWifiMethod("/queryBookByInfo")
//    public void queryBookByInfo(HttpServletRequest request, HttpServletResponse response) throws SQLException, IOException {
//        BookEntity bookByInfo = null;
//        String bookIdStr = request.getParameter("bookId");
//        String bookNameStr = request.getParameter("bookName");
//        String typeIdStr = request.getParameter("typeId");
//        String secondTypeIdStr = request.getParameter("secondTypeId");
//        String pageStart = request.getParameter("pageStart");
//        String pageSize = request.getParameter("pageSize");
//        String pageNumStr = request.getParameter("pageNum");
//        String pageSizeStr = request.getParameter("pageSize");
//        Integer integer = iBookService.booksCount();
//        PageInfo pageInfo = new PageInfo(pageNumStr, pageSizeStr, integer);
//        System.out.println(bookIdStr);
//        System.out.println(bookNameStr);
//        System.out.println(typeIdStr);
//        System.out.println(secondTypeIdStr);
//        if (StringUtil.isEmpty(bookIdStr)) {
//            if (StringUtil.isEmpty(bookNameStr)) {
//                if (StringUtil.isEmpty(typeIdStr)) {
//                    response.getWriter().write(JSON.toJSONString(1));
//                } else if (StringUtil.isEmpty(secondTypeIdStr)) {
//                    List<Object> list = iBookService.queryBookByType(Integer.valueOf(typeIdStr), pageInfo.getStart(), pageInfo.getPageSize());
//                    if (list != null && list.size() > 0) {
//                        pageInfo.setList(list);
//                        response.getWriter().write(JSON.toJSONString(pageInfo));
//                        return;
//                    } else {
//                        response.getWriter().write(JSON.toJSONString(1));
//                        return;
//                    }
//
//                } else {
//                    List<Object> list = iBookService.queryBookByTwoType(Integer.valueOf(typeIdStr), Integer.valueOf(secondTypeIdStr), pageInfo.getStart(), pageInfo.getPageSize());
//                    if (list != null && list.size() > 0) {
//                        pageInfo.setList(list);
//                        response.getWriter().write(JSON.toJSONString(pageInfo));
//                        return;
//                    } else {
//                        response.getWriter().write(JSON.toJSONString(1));
//                        return;
//                    }
//
//                }
//            } else {
//                List<Object> list = iBookService.queryBookByInfo(bookNameStr);
//                if (list != null && list.size() > 0) {
//                    pageInfo.setList(list);
//                    response.getWriter().write(JSON.toJSONString(pageInfo));
//                    return;
//                } else {
//                    response.getWriter().write(JSON.toJSONString(1));
//                    return;
//                }
//            }
//        } else {
//            bookByInfo = iBookService.queryBookByBookId(Integer.valueOf(bookIdStr));
//            if (bookByInfo != null) {
//                response.getWriter().write(JSON.toJSONString(bookByInfo));
//                return;
//            }
//            response.getWriter().write(JSON.toJSONString(1));
//        }
//
//    }
    @ShenZhouWifiMethod("/queryBookByInfo")
    public void queryBookByInfo(HttpServletRequest request, HttpServletResponse response) throws SQLException, IOException {
        BookEntity bookByInfo = null;
        String bookIdStr = request.getParameter("bookId");
        String bookNameStr = request.getParameter("bookName");
        String typeIdStr = request.getParameter("typeId");
        String secondTypeIdStr = request.getParameter("secondTypeId");
        String pageNumStr = request.getParameter("pageNum");
        String pageSizeStr = request.getParameter("pageSize");
        Integer integer = iBookService.booksCount();
        PageInfo pageInfo = new PageInfo(pageNumStr, pageSizeStr, integer);
        StringBuffer stringBuffer =new StringBuffer();
        if (!StringUtil.isEmpty(bookIdStr)){
            bookByInfo = iBookService.queryBookByBookId(Integer.valueOf(bookIdStr));
            if (bookByInfo != null) {
                response.getWriter().write(JSON.toJSONString(bookByInfo));
                return;
            }else {
                response.getWriter().write(JSON.toJSONString(0));
            }
        }
        int count = 0;
        if (!StringUtil.isEmpty(bookNameStr)){
            count++;
            stringBuffer.append(" book_name like ").append("'%"+bookNameStr+"%' and ");
        }
        if (!StringUtil.isEmpty(typeIdStr)){
            count++;
            stringBuffer.append(" ftype_id = "+typeIdStr+" and ");
        }
        if (!StringUtil.isEmpty(secondTypeIdStr)){
            count++;
            stringBuffer.append(" stype_id = "+secondTypeIdStr+" and ");
        }
        String string = stringBuffer.toString();
        String substring = string.substring(0, string.length() - 4);
        System.out.println(stringBuffer.toString());
        System.out.println(substring);
        List<Object> list = iBookService.queryBookByInfo(substring);
        if (list!=null&&list.size()>0){
            pageInfo.setList(list);
            response.getWriter().write(JSON.toJSONString(pageInfo));
        }else {
            response.getWriter().write(JSON.toJSONString(0));
        }
    }
}
